package com.lottery.common.aop;

import com.alibaba.fastjson.JSON;
import com.lottery.common.contants.ReQueryResult;
import com.lottery.common.utils.MyException;
import com.lottery.common.utils.XssUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

import static com.lottery.common.contants.Constants.TYPE_ZERO_ZERO_STR;

@Aspect
@Component
@Slf4j
public class CommonAop {
    /**
     * 指定切点
     * 匹配 com.example.demo.controller包及其子包下的所有类的所有方法
     */
    @Pointcut("@annotation(com.lottery.common.aop.Action)")
    public void webLog(){

    }



    @After("webLog()")
    public void after(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("___________");
        Action action = method.getAnnotation(Action.class);
        System.out.println("注解式拦截 "+action.name());
    }

    //后置通知
    @AfterReturning(returning = "ret", pointcut = "webLog()")
    public void afterReturning(Object ret) throws Throwable {
        // 处理完请求，返回内容
        log.info("——request  param: " + ret);
        ReQueryResult result = XssUtils.XssFilltration2(JSON.toJSONString(ret));
        XssUtils.sql_xss_inj(JSON.toJSONString(ret));
    }

    @Before("webLog()")
    public void before(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法规则式拦截,"+method.getName());
    }
}
